/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package r2x;

import java.util.LinkedList;
import java.util.ListIterator;

/**
 *
 * @author przemek
 */
public class DTDElement {

private String name;
private String cardinality;
private LinkedList<DTDElement> childrenList;
private int type;

public static int ERR = -1;
public static int ROOT = 0;
public static int TAB = 1;
public static int COL = 2;
public static int NNREL = 3;

public DTDElement(){
    childrenList = new LinkedList<DTDElement>();
    name = "";
    cardinality = DTDCreator.EXACTLYONE;
    type = ERR;
}

public DTDElement (TabInfo src){
    this();
    name = src.getTabName();
    type = TAB;
    cardinality = DTDCreator.ZEROORMORE;
    LinkedList<ColumnInfo> colList = src.getColumnsList();
    for (int i=0; i<colList.size(); i++){
        childrenList.add(new DTDElement(colList.get(i)));
    }
}

public DTDElement (ColumnInfo src){
    this();
    name = src.getName();
    type = COL;
    if (src.getRequesite() == true)
        cardinality = DTDCreator.EXACTLYONE;
    else
        cardinality = DTDCreator.ZEROORONE;
}

public DTDElement(String name, String card) throws Exception{
    this();
    this.name = name;
    if ((card.equals(DTDCreator.EXACTLYONE)) || (card.equals(DTDCreator.ONEORMORE))
            || (card.equals(DTDCreator.ZEROORMORE)) || (card.equals(DTDCreator.ZEROORONE)))
        cardinality = card;
    else throw new Exception("Wrong cardinality");
}

public DTDElement(String name, String card, int _type) throws Exception{
    this();
    this.name = name;
    if ((card.equals(DTDCreator.EXACTLYONE)) || (card.equals(DTDCreator.ONEORMORE))
            || (card.equals(DTDCreator.ZEROORMORE)) || (card.equals(DTDCreator.ZEROORONE)))
        cardinality = card;
    else throw new Exception("Wrong cardinality");
    type = _type;
}

public void addChild (DTDElement el){
    childrenList.add(el);
}

@Override
public String toString(){
    String result = "";
    return result;
}

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the cardinality
     */
    public String getCardinality() {
        return cardinality;
    }

    /**
     * @param cardinality the cardinality to set
     */
    public void setCardinality(String cardinality) {
        this.cardinality = cardinality;
    }

    /**
     * @return the childrenList
     */
    public LinkedList<DTDElement> getChildrenList() {
        return childrenList;
    }

    /**
     * @param childrenList the childrenList to set
     */
    public void setChildrenList(LinkedList<DTDElement> childrenList) {
        this.childrenList = childrenList;
    }

    /**
     * @return the type
     */
    public int getType() {
        return type;
    }

    /**
     * @param type the type to set
     */
    public void setType(int type) {
        this.type = type;
    }

    public DTDElement getChildByName (String _name){
        ListIterator<DTDElement> it = childrenList.listIterator();
        DTDElement el;
        while (it.hasNext()){
            el = it.next();
            if (el.name.equals(_name)){
                return el;
            }
        }
        return null;
    }
}
